<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-11-12 13:46
 *
 * 项目：rm  -  $  - ck_settings___maxnum.php
 *
 * 作者：liwei 
 */

namespace modules\levfm\table\form\FormData\validations;

use Lev;
use modules\levfm\table\form\FormData\formValidations;
use modules\levfm\table\LevfmDatasChild;

!defined('INLEV') && exit('Access Denied LEV');

class cks_settings___maxnum
{

    public static function errorMessage($setvals, $access) {
        foreach ($access as $v) {
            if (isset($setvals[$v['inputname']])) {
                if (method_exists(__CLASS__, $method = 'ck_'.$v['inputname'])) {
                    if ($errMsg = static::$method($setvals[$v['inputname']])) {
                        return $errMsg;
                    }
                }
            }
        }

    }

    public static function ck_isAdmin($setval) {
        if (!$setval && !Lev::$app['isAdmin']) {
            return Lev::responseMsg(-505010, '抱歉，你不是管理员，无权操作');
        }
    }

    public static function ck_disableEdit($setval) {
        if (!$setval && !Lev::$app['isAdmin']) {
            return Lev::responseMsg(-505009, '抱歉，禁止编辑数据');
        }
    }

    /**
     * 总发布限 留空不限，所有用户发布总和
     * @param $setval
     * @return array
     */
    public static function ck_maxs($setval) {
        if ($setval && formValidations::$formInfo['totalnum'] >= $setval) {
            return Lev::responseMsg(-505008, '抱歉，已达上限');
        }
    }

    /**
     * 每人限发上限 留空不限，达到上限禁止发布
     * @param $setval
     * @return array
     */
    public static function ck_maxone($setval) {
        if ($setval) {
            $formid = formValidations::$formInfo['id'];
            $where = 'uid =' . Lev::$app['uid'] . ' AND formid=' . $formid;
            $findAll = LevfmDatasChild::findAllField('id', $where);
            if (count($findAll) >= $setval) {
                return Lev::responseMsg(-505007, '抱歉，您已达上限了');
            }
        }
    }

    /**
     * 留空不限，每个用户每周最多可发数量
     * @param $setval
     * @return array
     */
    public static function ck_maxweek($setval) {
        if ($setval) {
            $formid = formValidations::$formInfo['id'];
            $where = 'uid =' . Lev::$app['uid'] . ' AND formid=' . $formid . ' AND uptime>' . Lev::getWeekStartTime();
            $findAll = LevfmDatasChild::findAllField('id', $where);
            if (count($findAll) >= $setval) {
                return Lev::responseMsg(-505006, '抱歉，您本周发布已达上限');
            }
        }
    }

    /**
     * 留空不限，每个用户每天最多可发数量
     * @param $setval
     * @return array
     */
    public static function ck_maxday($setval) {
        if ($setval) {
            $formid = formValidations::$formInfo['id'];
            $where = 'uid =' . Lev::$app['uid'] . ' AND formid=' . $formid . ' AND uptime>' . Lev::getDayStartTime();
            $findAll = LevfmDatasChild::findAllField('id', $where);
            if (count($findAll) >= $setval) {
                return Lev::responseMsg(-505005, '抱歉，您今日发布已达上限');
            }
        }
    }
}